概述
整合一:EurekaServer(单机版,可自己集群)
用的是Maven,pom文件如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud.eureka1</groupId>
<artifactId>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<version>2.0.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件如下,加入了Eureka认证所以有认证的用户名与密码(对应spring.security.user 配置)
eureka.client.serviceUrl.defaultZone是注册中心地址
eureka.client.fetchRegistry与registerWithEureka 表示是否将自己注册到注册中心与获取注册信息,因为本身是EurekaServer故不需要
server:
port: 8090
eureka:
client:
healthcheck:
enabled: true
service-url:
defaultZone: http://localhost:8090/eureka/
fetch-registry: false
register-with-eureka: false
spring:
security:
user:
name: lee
password: woaichinuomi
将csrf关闭,如果eureka客户端一直注册不上可能是这个问题,将这个类加上
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
SpringBootApplication启动类
@SpringBootApplication
@EnableEurekaServer
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
整合二,Eureka Client 将服务注册到EurekaServer中去
Pom文件如下
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>euredemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>euredemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</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>
application.yml配置
spring.application.name很重要这是服务的名字
datasource是数据源
jpa相关配置可以不要,我这里是为了省略自己建表
server:
port: 9090
spring:
application:
name: serviceUserProvider
datasource:
url: jdbc:mysql://localhost:3306/lee?serverTimezone=UTC
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database: MYSQL
show-sql: true
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
eureka:
client:
service-url:
defaultZone: http://lee:woaichinuomi@localhost:8090/eureka/
instance:
prefer-ip-address: true
自己随意编写一个服务提供类的Controller这里就是User相关的
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(value = "/getUserByName",produces = {"application/json;charset=UTF-8"})
public User getUser(@RequestParam(value = "username",defaultValue = "lee") String userName)
{
return userService.getUser(userName);
}
}
SpringBoot启动类
@SpringBootApplication
@EnableEurekaClient
public class EuredemoApplication {
public static void main(String[] args) {
SpringApplication.run(EuredemoApplication.class, args);
}
}
Eureka Client1 复制一个跟上面一个一模一样的项目包括yml中spring.application.name都一样的服务注册到注册中心上去,用于后面的Ribbon负载均衡qi启动这三个项目,去localhost:8090/eureka/去看
这样服务就注册上去了,注册中心中有两个服务提供者,服务的名字都叫serviceUserProvider并且一个端口是9090,一个是8090.
整合Feign、Ribbon、Hystrix
Pom如下
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.feign</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
application.yml配置
feign.hystrix.enabled默认是关闭的现在开启它
server:
port: 9099
spring:
application:
name: feign-ribbon-hystrix
feign:
hystrix:
enabled: true
eureka:
client:
service-url:
defaultZone: http://lee:woaichinuomi@localhost:8090/eureka/
Feign对应的接口
@FeignClient 中的 value 表明这个接口中的对应着注册中心的serviceUserProvider服务
fallback指定容错处理的类
@FeignClient(value = "serviceUserProvider" ,fallback = FeignServiceHiHystric.class)
public interface FeignServiceHi {
@GetMapping(value="/getUserByName")
String hi();
}
Hystrix
@Component
public class FeignServiceHiHystric implements FeignServiceHi {
@Override
public String hi() {
return "sorry the server is to busy";
}
}
提供一个Controller
@RestController
public class FeignServiceContro {
@Autowired
FeignServiceHi feignServiceHi;
@GetMapping("/hi")
public String sayHi()
{
return feignServiceHi.hi();
}
}
SpringBoot启动类
@EnableDiscoveryClient从注册中心发现服务
@EnableFeignClients启用Feign
@EnableHystrix启动hystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableEurekaClient
@EnableHystrix
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
启动这个项目,访问localhost:9099/hi
负载均衡到了两个服务提供服务,并且如果将这两个服务关闭模拟故障发现hystrix容错生效。
最后
以上就是温柔秋天为你收集整理的SpringCloud 整合Eureka、Ribbon、Feign、Hystrix整合一:EurekaServer(单机版,可自己集群)整合二,Eureka Client 将服务注册到EurekaServer中去Eureka Client1 复制一个跟上面一个一模一样的项目包括yml中spring.application.name都一样的服务注册到注册中心上去,用于后面的Ribbon负载均衡qi启动这三个项目,去localhost:8090/eureka/去看 整合Feign、Ribbon、H的全部内容,希望文章能够帮你解决SpringCloud 整合Eureka、Ribbon、Feign、Hystrix整合一:EurekaServer(单机版,可自己集群)整合二,Eureka Client 将服务注册到EurekaServer中去Eureka Client1 复制一个跟上面一个一模一样的项目包括yml中spring.application.name都一样的服务注册到注册中心上去,用于后面的Ribbon负载均衡qi启动这三个项目,去localhost:8090/eureka/去看 整合Feign、Ribbon、H所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复