创建用户管理服务消费者
新建项目
添加自述文件
克隆到本地
复制ignore文件
新建pom.xml
<?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>com.suntong</groupId>
<artifactId>myshop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../myshop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>myshop-service-user-consumer</artifactId>
<packaging>jar</packaging>
<url>http://www.suntong.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Starter Settings -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
<!-- Projects Settings -->
<dependency>
<groupId>com.suntong</groupId>
<artifactId>myshop-commons-dubbo</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.suntong</groupId>
<artifactId>myshop-static-backend</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.suntong</groupId>
<artifactId>myshop-service-user-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.suntong.myshop.service.user.consumer.MyShopServiceUserConsumerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
intelliJ托管
创建文件夹
创建包
创建Application
package com.suntong.myshop.service.user.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import sun.applet.Main;
@SpringBootApplication(scanBasePackages = "com.suntong.myshop")
@EnableHystrix
@EnableHystrixDashboard
public class MyShopServiceUserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(MyShopServiceUserConsumerApplication.class, args);
}
}
创建controller包
package com.suntong.myshop.service.user.consumer.controller;
import com.suntong.myshop.commons.domain.TbUser;
import com.suntong.myshop.service.user.api.TbUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping(value = "user")
public class TbUserController {
@Reference(version = "${services.versions.user.v1}")
private TbUserService tbUserService;
@RequestMapping(value = "list", method = RequestMethod.GET)
public String list(Model model){
List<TbUser> tbUsers = tbUserService.selectAll();
model.addAttribute("tbUsers", tbUsers);
return "user/list";
}
}
创建static、template目录再resources下
新建list.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:each="tbUser: ${tbUsers}">
<div th:text="${tbUser.username}"></div>
</div>
</body>
</html>
新建application.yml
# Spring boot application
spring:
application:
name: myshop-service-user-consumer
thymeleaf:
cache: false # 开发时关闭缓存,不然没法看到实时页面
mode: LEGACYHTML5 # 用非严格的 HTML
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8601
# Services Versions
services:
versions:
user:
v1: 1.0.0
# Dubbo Config properties
dubbo:
scan:
basePackages: com.suntong.myshop.service.user.consumer
## ApplicationConfig Bean
application:
id: myshop-service-user-consumer
name: myshop-service-user-consumer
## ProtocolConfig Bean
protocol:
id: dubbo
name: dubbo
port: 30881
status: server
serialization: kryo
## RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://192.168.79.136:2181?backup=192.168.79.136:2182,192.168.79.136:2183
# Dubbo Endpoint (default status is disable)
endpoints:
dubbo:
enabled: true
management:
server:
port: 8701
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
# Enables Dubbo All Endpoints
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
endpoints:
web:
exposure:
include: "*"
# Apollo
apollo:
bootstrap:
enabled: true
# will inject 'application' and 'TEST1.apollo' namespaces in bootstrap phase
namespaces: application
删除domain中实体类的前缀,实现序列化
重新install
这里的lifecycle是maven项目的生命周期,从clean到deploy,再到clean
Maven 的 Snapshot 版本与 Release 版本
1、Snapshot 版本代表不稳定、尚处于开发中的版本。
2、Release 版本则代表稳定的版本。
3、什么情况下该用 SNAPSHOT?
协同开发时,如果 A 依赖构件 B,由于 B 会更新,B 应该使用 SNAPSHOT 来标识自己。这种做法的必要性可以反证如下:
a. 如果 B 不用 SNAPSHOT,而是每次更新后都使用一个稳定的版本,那版本号就会升得太快,每天一升甚至每个小时一升,这就是对版本号的滥用。
b.如果 B 不用 SNAPSHOT, 但一直使用一个单一的 Release 版本号,那当 B 更新后,A 可能并不会接受到更新。因为 A 所使用的 repository 一般不会频繁更新 release 版本的缓存(即本地 repository),所以B以不换版本号的方式更新后,A在拿B时发现本地已有这个版本,就不会去远程Repository下载最新的 B
4、 不用 Release 版本,在所有地方都用 SNAPSHOT 版本行不行?
不行。正式环境中不得使用 snapshot 版本的库。 比如说,今天你依赖某个 snapshot 版本的第三方库成功构建了自己的应用,明天再构建时可能就会失败,因为今晚第三方可能已经更新了它的 snapshot 库。你再次构建时,Maven 会去远程 repository 下载 snapshot 的最新版本,你构建时用的库就是新的 jar 文件了,这时正确性就很难保证了。
启动后重新访问
注意先启动provider,再启动consumer
注意在consumer的Application的SpringBootApplication中加入
, exclude = DataSourceAutoConfiguration.class
不然会报错资源文件不存在
从报错信息中,我们就可以分析出错误原因是触发了数据源的自动化配置,然而当前项目其实并不需要数据源。查其根源是依赖方提供的API依赖中引用了一些多余的依赖触发了该自动化配置的加载。
如何解决
为了解决上面所述的问题,我们可以用两种方法来解决:
通过外部依赖的修改来解决:通过与依赖方沟通,在对方提供的API依赖中去掉不必要的依赖
通过禁用指定的自动化配置来避免加载不必要的自动化配置,下面列举了禁用的方法:
使用了@EnableAutoConfiguration的时候
1 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
使用了@SpringBootApplication的时候
1 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
使用了@SpringCloudApplication的时候
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringCloudApplication
通过配置文件来设置
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
在consumer中不需要数据源自动化配置,也没有配置文件,但不知道哪个依赖耍流氓非要配置,那我们就可以忽略它。
网友评论