Spring Cloud 学习笔记
Spring Cloud 学习笔记
xiaoye08050141 发表于6个月前
Spring Cloud 学习笔记
  • 发表于 6个月前
  • 阅读 184
  • 收藏 7
  • 点赞 0
  • 评论 0

华为云·免费上云实践>>>   

工程搭建

1、http://start.spring.io/ 

   

下载工程

2、import 工程

修改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>

	<groupId>org.xiaoye.clearworld</groupId>
	<artifactId>clearworld-eureka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>clearworld-eureka</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.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>Dalston.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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>

3、服务注册中心:新建maven模块【clearworld-eureka-register】

修改pom文件

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	
	<parent>
		<groupId>org.xiaoye.clearworld</groupId>
		<artifactId>clearworld-eureka</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<artifactId>clearworld-eureka-register</artifactId>
	<name>clearworld-eureka-register</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies></dependencies>
</project>

增加配置application.properties

## 端口号
server.port = 8000
## session 超时时间
server.sessionTimeout = 30

eureka.instance.hostname = localhost
eureka.client.registerWithEureka = false
eureka.client.fetchRegistry = false
eureka.client.serviceUrl.defaultZone = http://${eureka.instance.hostname}:${server.port}/eureka/

创建启动类

@EnableEurekaServer
@SpringBootApplication
public class AppStarter {
    
    public static void main(String[] args) {
        SpringApplication.run(AppStarter.class, args);
    }
}

4、服务提供方:新建maven模块【clearworld-eureka-server】

修改pom文件

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>org.xiaoye.clearworld</groupId>
		<artifactId>clearworld-eureka</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>clearworld-eureka-server</artifactId>
	<name>clearworld-eureka-server</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<dependencies></dependencies>
</project>

增加配置application.properties

## 系统名称
application.system-name = eureka-server
## 端口号
server.port = 8001
## session 超时时间
server.sessionTimeout = 30

spring.application.name = ${application.system-name}
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

增加启动类:

@EnableEurekaClient
@SpringBootApplication
public class AppStarter {
    
    public static void main(String[] args) {
        SpringApplication.run(AppStarter.class, args);
    }
}

增加Controller

@RestController
public class TestController {

    @RequestMapping("hello")
    public String hello(@RequestParam String name) {
        return "你好," + name;
    }
}

5、服务消费方:新建maven模块【clearworld-eureka-client】

修改pom文件

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	
	<parent>
		<groupId>org.xiaoye.clearworld</groupId>
		<artifactId>clearworld-eureka</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<artifactId>clearworld-eureka-ribbon</artifactId>
	<name>clearworld-eureka-ribbon</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>
	</dependencies>
</project>

增加配置application.properties

## 系统名称
application.system-name = eureka-client
## 端口号
server.port = 8004
## session 超时时间
server.sessionTimeout = 30

spring.application.name = ${application.system-name}
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

增加启动类

@EnableDiscoveryClient
@SpringBootApplication
public class AppStarter {
    
    public static void main(String[] args) {
        SpringApplication.run(AppStarter.class, args);
    }
    
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

增加Controller

// Controller 控制器
@RestController
public class TestController {

    @Autowired
    private TestService service;
    
    @RequestMapping("execute")
    public String execute() {
        String message = service.execute();
        return message;
    }
}

// 服务接口类
public interface TestService {

    public String execute();

}

// 服务接口实现类
@Service
public class TestServiceImpl implements TestService {
    
    @Autowired
    RestTemplate template;

    @Override
    public String execute() {
        return template.getForObject("http://EUREKA-SERVER/hello?name='叶生'", String.class);
    }
}

6、测试

启动说明:

    a、启动服务注册中心,端口号:8000

    b、启动服务提供方,端口号:8001

    c、启动服务提供方,端口号:8002

    d、启动服务消费方,端口号:8004

访问:http://localhost:8000/  打开服务中心

访问:http://localhost:8004/execute

返回:你好,'叶生'

 

结束。

 

异常解析:

1、服务提供方只启动一次(这里只启动端口:8001的服务)

访问:http://localhost:8004/execute

服务消费方后台异常:java.lang.IllegalStateException: No instances available for EUREKA-SERVER

解决:

1、解决方案一:再启动一个服务提供方

2、解决方案二:

    a、取消服务消费方启动类中的“@LoadBalanced”注解(此为负载均衡注解,个人认为需要多个服务,具体待了解)

    b、修改请求服务提供方的地址为:

return template.getForObject("http://localhost:8001/hello?name='叶生'", String.class);

 

共有 人打赏支持
粉丝 0
博文 15
码字总数 10838
×
xiaoye08050141
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: