1.Eureka在上一章節(jié)我們已經(jīng)創(chuàng)建成功,初始化的Eureka是沒有服務(wù)注冊(cè)的。那么這一章節(jié)就開始創(chuàng)建服務(wù)提供者。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<!--區(qū)別一下Eureka注冊(cè)中心 依賴的是server的jar包、服務(wù)提供者的依賴包是client~~-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.eureka;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
//消費(fèi)者提供
@SpringBootApplication
@EnableEurekaClient //在程序類啟動(dòng)的時(shí)候像eureka注冊(cè)中心注冊(cè)一個(gè)服務(wù)
@RestController //restful注解
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
//端口號(hào)
@Value("${server.port}")
private String port;
//提供一個(gè)hi的測(cè)試方法
@RequestMapping(value = "/hi")
private String sayHei(){
return "hello Word"+port+"test";
}
}
yml配置文件配置的相關(guān)信息
server:
port: 8091 #設(shè)置端口號(hào)
spring:
application:
name: service-hi #后續(xù)通過(guò)這里配置的名字進(jìn)行調(diào)用服務(wù)
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #表名自己的服務(wù)中心的地址
注冊(cè)完會(huì)看到一個(gè)叫service-hi的服務(wù)端口號(hào)為8091 那么就代表我們的服務(wù)注冊(cè)成功了
image.png
服務(wù)消費(fèi)者(Feign與、RestTemplate的調(diào)用)
Feign的調(diào)用
//在SpringBootApplication啟動(dòng)類上打上@EnableFeignClients注解
package com.example.servicefeignapplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients//開啟feign服務(wù)
public class ServicefeignapplicationApplication {
public static void main(String[] args) {
SpringApplication.run(ServicefeignapplicationApplication.class, args);
}
}
創(chuàng)建一個(gè)service接口類去調(diào)用服務(wù)
package com.example.servicefeignapplication.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Auther: 創(chuàng)建一個(gè)接受服務(wù)的接口 FeignClinet表示調(diào)用的服務(wù)
* @Date: 2018/7/18 0018 09:54
* @Description:
*/
@FeignClient(value = "service-hi")
public interface ServiceTest {
//調(diào)用服務(wù)名為service-hi的服務(wù)方法
@RequestMapping(value = "/hi")
public String say();
}
給web層暴露一個(gè)controller可以進(jìn)行訪問(wèn)方法
package com.example.servicefeignapplication.controller;
import com.example.servicefeignapplication.service.ServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Auther: TG
* @Date: 2018/7/18 0018 09:56
* @Description:
*/
@RestController
public class ServiceController {
//這里會(huì)進(jìn)行報(bào)錯(cuò),啟動(dòng)沒有任何問(wèn)題 原因是因?yàn)闊o(wú)法識(shí)別 差找不到對(duì)應(yīng)的bean
@Autowired
private ServiceTest serviceTest;
@GetMapping(value = "hello")
public String hello(){
System.out.println(serviceTest.say());
return serviceTest.say();
}
}
yml文件的配置
server:
port: 8888
spring:
application:
name: serverBase 服務(wù)名
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.采用RestTemplate調(diào)用服務(wù)
啟動(dòng)類
package com.example.serviceribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
//開啟負(fù)載均衡
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
service
package com.example.serviceribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @Auther: TG
* @Date: 2018/7/17 0017 15:06
* @Description:
*/
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
//調(diào)用service服務(wù)中心的數(shù)據(jù)
public String getMessage(){
System.out.println("調(diào)用注冊(cè)的服務(wù)--->");
return restTemplate.getForObject("http://service-hi/hi",String.class);
}
}
package com.example.serviceribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Auther: TG
* @Date: 2018/7/17 0017 15:09
* @Description:
*/
@RestController
public class RibbonController {
@Autowired
private RibbonService ribbonService;
@RequestMapping(value = "sayBase")
public String sayBase(){
return ribbonService.getMessage();
}
}
yml配置文件配置
server:
port: 80 #設(shè)置端口為80
eureka:
client:
serviceUrl:
defaultZone : http://localhost:8761/eureka/
spring:
application:
name: service-ribbon
@EnableEurekaClient采用Eureka服務(wù)注冊(cè)中心的時(shí)候采用
@EnableDiscoveryClient使用其他服務(wù)注冊(cè)中心的時(shí)候采用