1.Eureka在上一章節(jié)我們已經創(chuàng)建成功,初始化的Eureka是沒有服務注冊的。那么這一章節(jié)就開始創(chuàng)建服務提供者。
<?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注冊中心 依賴的是server的jar包、服務提供者的依賴包是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;
//消費者提供
@SpringBootApplication
@EnableEurekaClient //在程序類啟動的時候像eureka注冊中心注冊一個服務
@RestController //restful注解
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
//端口號
@Value("${server.port}")
private String port;
//提供一個hi的測試方法
@RequestMapping(value = "/hi")
private String sayHei(){
return "hello Word"+port+"test";
}
}
yml配置文件配置的相關信息
server:
port: 8091 #設置端口號
spring:
application:
name: service-hi #后續(xù)通過這里配置的名字進行調用服務
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #表名自己的服務中心的地址
注冊完會看到一個叫service-hi的服務端口號為8091 那么就代表我們的服務注冊成功了
image.png
服務消費者(Feign與、RestTemplate的調用)
Feign的調用
//在SpringBootApplication啟動類上打上@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服務
public class ServicefeignapplicationApplication {
public static void main(String[] args) {
SpringApplication.run(ServicefeignapplicationApplication.class, args);
}
}
創(chuàng)建一個service接口類去調用服務
package com.example.servicefeignapplication.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Auther: 創(chuàng)建一個接受服務的接口 FeignClinet表示調用的服務
* @Date: 2018/7/18 0018 09:54
* @Description:
*/
@FeignClient(value = "service-hi")
public interface ServiceTest {
//調用服務名為service-hi的服務方法
@RequestMapping(value = "/hi")
public String say();
}
給web層暴露一個controller可以進行訪問方法
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 {
//這里會進行報錯,啟動沒有任何問題 原因是因為無法識別 差找不到對應的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 服務名
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.采用RestTemplate調用服務
啟動類
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);
}
//開啟負載均衡
@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;
//調用service服務中心的數據
public String getMessage(){
System.out.println("調用注冊的服務--->");
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 #設置端口為80
eureka:
client:
serviceUrl:
defaultZone : http://localhost:8761/eureka/
spring:
application:
name: service-ribbon
@EnableEurekaClient采用Eureka服務注冊中心的時候采用
@EnableDiscoveryClient使用其他服務注冊中心的時候采用